#include "Display_EPD_W21_spi.h"
#include "systick.h"



void epd_delay(uint16_t ms)
{
  delay_1ms(ms);
}

void epd_res_set(void)
{
  gpio_bit_write(GPIOC, GPIO_PIN_6, SET);
}

void epd_res_reset(void)
{
  gpio_bit_write(GPIOC, GPIO_PIN_6, RESET);
}

void epd_dc_set(void)
{
  gpio_bit_write(GPIOB, GPIO_PIN_12, SET);
}

void epd_dc_reset(void)
{
  gpio_bit_write(GPIOB, GPIO_PIN_12, RESET);
}

void epd_cs_set(void)
{
  gpio_bit_write(GPIOB, GPIO_PIN_14, SET);
}

void epd_cs_reset(void)
{
  gpio_bit_write(GPIOB, GPIO_PIN_14, RESET);
}

uint8_t epd_is_busy(void)
{
  return gpio_input_bit_get(GPIOC, GPIO_PIN_7) == RESET ? 0 : 1;
}

#if 0
//E-paper GPIO initialization
void EPD_GPIO_Init(void)
{
  rcu_periph_clock_enable(RCU_GPIOC);
  rcu_periph_clock_enable(RCU_GPIOB);
  rcu_periph_clock_enable(RCU_AF);

  spi_parameter_struct spi_init_struct;

  /* configure the epaper module res pin */
  gpio_init(GPIOC, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);

  /* configure the epaper module busy pin */
  gpio_init(GPIOC, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_7);

  /* configure the epaper module d/c pin */
  gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_14);

  /* configure the epaper module SPI1 sck mosi pin */
  gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13 | GPIO_PIN_15);

  /* configure the epaper module cs pin */
  gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);

  /* configure SPI1 */
  rcu_periph_clock_enable(RCU_SPI1);

  /* deinitilize SPI and the parameters */
  spi_i2s_deinit(SPI1);
  spi_struct_para_init(&spi_init_struct);

  spi_init_struct.trans_mode = SPI_TRANSMODE_BDTRANSMIT;
  spi_init_struct.device_mode = SPI_MASTER;
  spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
  spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
  spi_init_struct.nss = SPI_NSS_SOFT;
  spi_init_struct.prescale = SPI_PSC_128;
  spi_init_struct.endian = SPI_ENDIAN_MSB;
  spi_init(SPI1, &spi_init_struct);

  spi_enable(SPI1);
}



//SPI write byte
void SPI_Write(unsigned char value)
{				   			 
    spi_i2s_data_transmit(SPI1, value);
    while (spi_i2s_flag_get(SPI1, SPI_FLAG_TBE) == RESET)
        ;
    while (spi_i2s_flag_get(SPI1, SPI_FLAG_TRANS) != RESET)
        ;
}

//SPI write command
void EPD_W21_WriteCMD(unsigned char command)
{
  epd_dc_reset();
  epd_cs_reset();

  spi_i2s_data_transmit(SPI1, command);
  while (spi_i2s_flag_get(SPI1, SPI_FLAG_TBE) == RESET)
    ;
  while (spi_i2s_flag_get(SPI1, SPI_FLAG_TRANS) != RESET)
    ;

  epd_cs_set();
  epd_dc_set();
}
//SPI write data
void EPD_W21_WriteDATA(unsigned char datas)
{
  epd_cs_reset();

  spi_i2s_data_transmit(SPI1, datas);
  while (spi_i2s_flag_get(SPI1, SPI_FLAG_TBE) == RESET)
    ;
  while (spi_i2s_flag_get(SPI1, SPI_FLAG_TRANS) != RESET)
    ;

  epd_cs_set();
}


#endif

#if 1

void EPD_GPIO_Init(void)
{
  rcu_periph_clock_enable(RCU_GPIOC);
  rcu_periph_clock_enable(RCU_GPIOB);
  rcu_periph_clock_enable(RCU_AF);

  spi_parameter_struct spi_init_struct;

  /* configure the epaper module res pin */
  gpio_init(GPIOC, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);

  /* configure the epaper module busy pin */
  gpio_init(GPIOC, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_7);

  /* configure the epaper module d/c pin */
  gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_14);

  /* configure the epaper module SPI1 sck mosi pin */
  gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13 | GPIO_PIN_15);

  /* configure the epaper module cs pin */
  gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
  
  gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
}

void SPI_Delay(unsigned char xrate)
{
	while(xrate--);

}

void SPI_Write(unsigned char value)                                    
{                                                           
	unsigned char i;
	for(i=0; i<8; i++)   
	{
		EPD_W21_CLK_0;
		if(value & 0x80)
			EPD_W21_MOSI_1;
		else
			EPD_W21_MOSI_0;		
		value = (value << 1);			
		EPD_W21_CLK_1;		
	}
}

void EPD_W21_WriteCMD(unsigned char command)
{
  EPD_W21_CS_0;                   
	EPD_W21_DC_0;		// command write
	SPI_Write(command);
	EPD_W21_CS_1;
}
void EPD_W21_WriteDATA(unsigned char data)
{
  EPD_W21_CS_0;                   
	EPD_W21_DC_1;		// data write
	SPI_Write(data);
	EPD_W21_CS_1;
}

/***********************************************************
						end file
***********************************************************/


#endif



